package com.briup.product_my.service.impl;
import com.briup.product_my.domain.ManagerHurdles;
import com.briup.product_my.domain.ext.ManagerFenceHouseExt;
import com.briup.product_my.mapper.ManagerHurdlesMapper;
import com.briup.product_my.mapper.ext.ManagerHurdlesExtMapper;
import com.briup.product_my.service.HurdlesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xiongwenbin
 * @Date: 2024/8/22-08-22-上午10:37
 * @Description: com.briup.product_my.service.impl
 */
@Service
public class HurdlesServiceImpl implements HurdlesService {
    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;
    @Autowired
    private ManagerHurdlesExtMapper managerHurdlesExtMapper;
    @Override
    public List<Integer> queryAllMax() {

        return managerHurdlesMapper.queryAllMax();
    }


    @Override
    public void updateEnable(String hId, String hEnable) {
        if (hEnable.equals("可用")) {
            hEnable = "禁用";
        } else {

            hEnable = "可用";
        }
        managerHurdlesMapper.updateEnable(hId, hEnable);
    }

    @Override
    public void updateEnableBatch(List<ManagerHurdles> managerHurdles) {
        List<String> hIds = new ArrayList<>();
        List<String> hIds2 = new ArrayList<>();
        for (ManagerHurdles hurdles : managerHurdles) {
            if (hurdles.getHEnable().equals("可用")) {
                hIds.add(hurdles.getHId());
            } else {
                hIds2.add(hurdles.getHId());
            }
            if(!hIds.isEmpty()){
                managerHurdlesMapper.updateEnableBatch(hIds, "禁用");
            }
            if(!hIds2.isEmpty()){
                managerHurdlesMapper.updateEnableBatch(hIds2, "可用");
            }
        }
    }

    @Override
    public PageInfo<ManagerFenceHouseExt> selectAllBy(Integer pageNum, Integer pageSize, String hName, Integer hMax, String fhName, String hEnable) {
        PageHelper.startPage(pageNum, pageSize);
        List<ManagerFenceHouseExt> managerFenceHouseExts = managerHurdlesExtMapper.selectAllBy(hName, hMax, fhName, hEnable);
        return new PageInfo<>(managerFenceHouseExts);
    }

    @Override
    public ManagerHurdles saveOrUpdate(ManagerHurdles managerHurdles) {

        if (!StringUtils.hasText(managerHurdles.getHId())) {
            managerHurdles.setHId(managerHurdlesMapper.selectMaxId() + 1);
            managerHurdlesMapper.insert(managerHurdles);
            return managerHurdles;
        } else {
            managerHurdlesMapper.updateByPrimaryKeySelective(managerHurdles);
            return managerHurdles;
        }
    }

    @Override
    public void deleteById(String hId) {
        managerHurdlesMapper.deleteById(hId);
    }

    @Override
    public List<ManagerFenceHouseExt> queryAllEnable() {
        return managerHurdlesExtMapper.queryAllEnable();
    }


}